﻿#include "IdentifyDialog.h"
#include "ui_IdentifyDialog.h"

IdentifyDialog::IdentifyDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::IdentifyDialog)
{
    ui->setupUi(this);
    m_dbLoader.initFPCacheDB(&m_cacheDB);

    connect(&m_reader, SIGNAL(onFingerprintReceived(QImage,QByteArray)) , this, SLOT(onFingerprintReceived(QImage,QByteArray)));
    m_reader.openDevice();
    m_reader.beginCapture();
}

void IdentifyDialog::onFingerprintReceived(QImage image, QByteArray fpTemplate)
{
    ui->labelImage->setPixmap(QPixmap::fromImage(image));
    unsigned int fid;
    unsigned int score;
    if( m_cacheDB.identify(fpTemplate, fid, score) == 0)
    {
        QString name = m_dbLoader.userName(fid);
        ui->labelUserID->setText(QString::number(fid));
        ui->labelName->setText(name);
    }
    else
    {
        ui->labelUserID->setText("UNKNOWN");
        ui->labelName->setText("UNKNOWN");
    }
}

IdentifyDialog::~IdentifyDialog()
{
    if(m_reader.isRunning())
    {
        m_reader.cancelCapture();
        m_reader.wait(150);
    }
    m_reader.closeDevice();
    delete ui;
}
